
<?php
$file_name = "C:\\AppServ\\www\\szofttech_src\\workdir\\ARTIKELC.DBF";
$table = "dbf_apiss";

$dbf = @dbase_open($file_name, 0) or die("Error opening $file_name");
$fields = dbase_get_header_info($dbf);

/* Just show a table with header information */
echo "<table cellspacing=0><tr><th>Name</th><th>Type</th>
<th>Length</th><th>Precision</th><th>Format</th><th>Offset</th></tr>";
$op=0;
foreach($fields as $field)
{
    echo "<tr>
		<td>{$op}</td>
        <td>{$field['name']}</td>
        <td>{$field['type']}</td>
        <td>{$field['length']}</td>
        <td>{$field['precision']}</td>
        <td>{$field['format']}</td>
        <td>{$field['offset']}</td>
        </tr>";
		$op++;
}
echo "</table><br><br>";

/* Here begins the SQL code part */
echo "CREATE TABLE $table (<BR>";
$x = 0;
$fields_num = 0;
foreach($fields as $field)
{
    if($x++ != 0)
        echo ",<BR>";
    switch($field['type'])
    {
        case 'character' : $type = 'CHAR'; $length = $field['length'] > 1 ? "({$field['length']})" : ""; break;
        case 'number' : $type = 'NUMERIC'; $length = "({$field['length']}" . ($field['precision'] > 0 ? ", {$field['precision']})": ")");
        break;
//        case '' : $type = ''; break;
    }
    $fields_num++;
    echo "&nbsp;&nbsp;&nbsp;{$field['name']} $type$length";
}
echo ");<BR><BR>BEGIN;<BR>";
$records = @dbase_numrecords($dbf) or die("Error reading DBF's number of fields");
for($x = 1; $x <= 1/*$records*/; $x++)
{
    $record = dbase_get_record($dbf, $x);
    echo "INSERT INTO $table VALUES (";
    $f = 0;
    foreach($record as $field)
    {
        if($f != 0) echo ", ";
        if(strcmp(str_repeat(' ', $fields[$f]['length']), $field) != 0)
        {
            if($fields[$f]['type'] == 'character') echo "'";
            echo $f.$field;
            if($fields[$f]['type'] == 'character') echo "'";
        }
        else
            echo "NULL";
        if(++$f >= $fields_num) break;
    }
    echo ");<BR>";
}
echo "COMMIT;";
?>